{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "426b438f-9616-47d1-97a1-922b6db9f744",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-03T18:50:38.540758Z",
     "iopub.status.busy": "2022-02-03T18:50:38.540317Z",
     "iopub.status.idle": "2022-02-03T18:50:38.621539Z",
     "shell.execute_reply": "2022-02-03T18:50:38.620937Z",
     "shell.execute_reply.started": "2022-02-03T18:50:38.540732Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "893980db-aac6-4bc6-b37f-6ed6461e6738",
   "metadata": {},
   "source": [
    "1. 一维数组的索引切片操作与python的list一样\n",
    "2. 多维数组则以逗号分隔维度行列参数（以二维数组为例）\n",
    "    * 如果只有一个参数值，默认为行参数\n",
    "    * 行（列）参数内部的索引和切片操作参照一维数组方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3bef89b6-f968-4c35-82bf-c8f714526d60",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-03T18:50:41.166028Z",
     "iopub.status.busy": "2022-02-03T18:50:41.165776Z",
     "iopub.status.idle": "2022-02-03T18:50:41.171972Z",
     "shell.execute_reply": "2022-02-03T18:50:41.171239Z",
     "shell.execute_reply.started": "2022-02-03T18:50:41.166002Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组a1为：[0 1 2 3 4 5 6 7 8 9]\n",
      "第0个位置的元素为：0\n",
      "第4、5、6三个位置的元素为：[4 5 6]\n",
      "设置步长为2之后的数组为：[0 2 4 6 8]\n",
      "最后一个数组元素为：9\n"
     ]
    }
   ],
   "source": [
    "# 1. 一维数组的索引与切片\n",
    "a1 = np.arange(10)\n",
    "print(\"数组a1为：{}\".format(a1))\n",
    "\n",
    "# 1.1 进行索引操作\n",
    "print(\"第0个位置的元素为：{}\".format(a1[0]))  # 获取第0个位置的元素\n",
    "\n",
    "# 1.2 进行切片操作\n",
    "print(\"第4、5、6三个位置的元素为：{}\".format(a1[4:7]))  # 获取第4、5、6三个位置的元素\n",
    "\n",
    "# 1.3 使用步长\n",
    "print(\"设置步长为2之后的数组为：{}\".format(a1[::2]))  # 以步长2来遍历获取从第0位置开始直到末尾的所有元素\n",
    "\n",
    "# 1.4 使用负数做索引\n",
    "print(\"最后一个数组元素为：{}\".format(a1[-1]))  # 获取最后位置的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "68e8f578-edb6-4093-accb-4a0d580980c5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-02-03T19:21:49.412230Z",
     "iopub.status.busy": "2022-02-03T19:21:49.411782Z",
     "iopub.status.idle": "2022-02-03T19:21:49.417661Z",
     "shell.execute_reply": "2022-02-03T19:21:49.416894Z",
     "shell.execute_reply.started": "2022-02-03T19:21:49.412204Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 2. 多维数组(以二维数组为例)\n",
    "# 也是通过中括号来索引和切片，使用逗号进行分割，逗号前面是行，逗号后面是列\n",
    "a2 = np.random.randint(0, 10, size=(4, 6))\n",
    "print(\"数组a2为：\\n{}\".format(a2))\n",
    "\n",
    "# 2.1 只按行获取数据\n",
    "# 如果只有一个索引参数值，默认为指定行\n",
    "# 获取单行数据\n",
    "print(\"第0行元素为：{}\".format(a2[0])) # 获取第0行的所有元素\n",
    "# 获取连续行的数据\n",
    "print(\"第1、2行元素为：\\n{}\".format(a2[1:3])) # 获取第1、2行的所有元素\n",
    "# 获取不连续行的数据\n",
    "print(\"第0、2、3行元素为：\\n{}\".format(a2[[0,2,3]])) # 获取第0、2、3行的所有元素\n",
    "\n",
    "# 2.2 按行列获取数据\n",
    "# 获取不连续的数据\n",
    "print(\"第2行第2列的元素是：{}\".format(a2[2,2])) # 获取第2行第2列的元素\n",
    "print(\"第1行第4列和第2行第5列的元素是：{}\".format(a2[[1,2],[4,5]])) # 获取第1行第4列和第2行第5列的元素\n",
    "# print(\"{}\".format())\n",
    "# 获取连续的数据\n",
    "print(\"第1行到第2行，第4列到第5列的所有元素为：\\n{}\".format(a2[1:3, 4:6])) # 获取第1行到第2行，第4列到第5列的所有元素\n",
    "\n",
    "# 2.3 只按列获取数据\n",
    "# 获取单列数据\n",
    "print(\"第0列元素为：{}\".format(a2[:,0])) # 获取第0列的所有元素\n",
    "# 获取连续列的数据\n",
    "print(\"第1、2列元素为：\\n{}\".format(a2[:,1:3])) # 获取第1、2列的所有元素\n",
    "# 获取不连续列的数据\n",
    "print(\"第0、2、3列元素为：\\n{}\".format(a2[:,[0,2,3]])) # 获取第0、2、3列的所有元素"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
